<template>
	<view class="my_login">
		<view class="my_login_box">
			<view class="my_icon">
				<uni-icons type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
			</view>
			<button type="primary" class="btn-login" @click="getUserProfile">一键登录</button>
			<view class="my_text">
				<text>登录后尽享更多的权益</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapState,
		mapMutations
	} from 'vuex'

	export default {
		name: "my_login",
		data() {
			return {

			};
		},

		computed: {
			...mapState('m_user', ['redirectInfo']),
		},

		methods: {
			...mapMutations('m_user', ['updateUserinfo', 'updateToken', 'updateRedirectInfo']),

			// getUserInfo(e) {
			// 	console.log(e)
			// 	if (e.detail.errMsg === 'getUserInfo:fail auth deny') return uni.$showMes('您取消了登录授权！')

			// 	// 获取用户信息
			// 	// console.log(e.detail.userInfo)
			// 	this.updateUserinfo(e.detail.userInfo)

			// 	// 获取token
			// 	this.getToken(e.detail)
			// },

			getUserProfile(e) {
				// console.log(e)
				uni.getUserProfile({
					desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success: (res) => {
						this.updateUserinfo(res.userInfo)
						this.getToken(res)
					},
					fail() {
						uni.$showMes('您取消了登录授权')
					}
				})


			},

			async getToken(info) {
				const [err, res] = await uni.login().catch(err => err)
				if (err || res.errMsg !== 'login:ok') return uni.$showError('登录失败！')
				// console.log(res)
				const query = {
					code: res.code,
					aID: 'wx0300cf958defb188',
					secret: '',
					encryptedData: info.encryptedData,
					iv: info.iv,
					rawData: info.rawData,
					signature: info.signature
				}
				// 换取token
				uni.request({
					url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + query.aID + '&secret=' + query
						.secret + '&js_code=' + query.code + '&rant_type=authorization_code',
					success: (res) => {
						// 由于暂时没有后端接口，所以这个先暂时自己随机生成一个token值来进行储存(当然只是简单的进行随机数生成)
						// this.updateToken('')
						// console.log(res)
						if (res.data.errcode || res.data.errmsg ===
							"appsecret missing, rid: 61f93c47-42337fee-72b2b7cf" || res.data.errmsg ===
							"invalid appsecret, rid: 61f93d5f-064acaea-18758e2c") {
							return uni.$showMes('登录失败,密钥有误')
						}
						this.updateToken(Math.random() * 10000)
					}
				})

				this.navigeteBack()
				// const {
				// 	data: loginResult
				// } = await uni.$http.post('/api/public/v1/users/wxlogin', query)
				// console.log(loginResult)
				// if (loginResult.meta.status !== 200) return uni.$showMes('登录失败！')

				// this.updateToken(loginResult.message.token)

			},

			navigeteBack() {
				if (this.redirectInfo && this.redirectInfo.openType === 'switchTab') {
					uni.switchTab({
						url: this.redirectInfo.from,
						complete: () => {
							this.updateRedirectInfo(null)
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.my_login_box {
		// display: flex;
		// flex-direction: column;
		margin-top: 70px;
		padding: 30px;

		.my_icon {
			text-align: center;
		}

		.btn-login {
			border-radius: 50px;
			margin: 15px 0;
		}

		.my_text {
			text-align: center;
			font-size: 12px;
			color: #8b8b8b;
		}
	}
</style>
